<template>
  <div class="head-nav">
    <el-header>
      <div class="content">
        <citySelector></citySelector>
        <ul>
          <router-link to="/"><li>首页</li></router-link>

          <router-link to="/login" v-show="$store.getters.token == null">
            <li>您好，请登录</li>
          </router-link>
          <router-link to="/regist" v-show="$store.getters.token == null">
            <li>免费注册</li>
          </router-link>

          <li v-show="$store.getters.token != null">
            <el-dropdown @command="showChangePassDialog">
              <el-avatar :src="'http://124.223.89.139/pic/'+$store.getters.avatar" style=""></el-avatar>
              <el-dropdown-menu slot="dropdown" >
                <el-dropdown-item command="1">修改密码</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </li>

          <li
            @click="dologout"
            style="cursor:pointer"
            v-show="$store.getters.token != null"
          >
            登出
          </li>

          <li @click="checkIsLogin('order')" style="cursor:pointer">
            我的订单
          </li>
          <li @click="checkIsLogin('cart')" style="cursor:pointer">
            我的购物车
          </li>

          <li class="message">
            消息
            <el-badge :value="messageCount" class="item"> </el-badge>
          </li>
          <li>客户服务</li>
        </ul>
      </div>
    </el-header>
    <el-dialog title="修改密码" :visible.sync="showDialog">
    <el-form  ref="dataForm" :model="dataForm">
  <el-form-item  label="原密码">
    <el-input v-model="dataForm.oripass"></el-input>
  </el-form-item>
  <el-form-item  label="新密码">
    <el-input v-model="dataForm.newpass"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="changePass()">修改</el-button>
 <el-button @click="showDialog = false" >关闭</el-button>
  </el-form-item>
</el-form>
  </el-dialog>
  </div>
</template>

<script>
import citySelector from "./CitySelector.vue";
import {updatePassword} from '../api/User/user'
export default {
  components: {
    citySelector,
  },
  name: "BHeader",
  inject: ["reload"],
  data() {
    return {
      messageCount: "10",
      showDialog:false,
      dataForm:{}
    }
  },
  methods: {
    dologout() {
      this.$store
        .dispatch("FedLogOut")
        .then(() => {
          this.$router.go(0);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    checkIsLogin(e) {
      if (this.$store.getters.token == null) {
        this.$message.info("请先登录");
      } else if (e == "order") {
        this.$router.push("/orderlist");
      } else if (e == "cart") {
        this.$router.push("/car");
      }
    },
    showChangePassDialog(){
      this.showDialog = true;
    },
    changePass(){
      updatePassword(this.dataForm).then((res)=>{
        if(res){
          this.showDialog=false;
          this.dologout();
        }
      })
    }
  },
};
</script>

<style scoped>
.message {
  left: -10px;
  position: relative;
}
.item {
  position: absolute;
  top: 0px;
  left: 20px;
}

.content {
  margin: 0 auto;
  display: flex;
  width: 1320px;
  height: 40px;
}
ul {
  list-style: none;
  height: 40px;
  position: absolute;
  left: 65%;
  font-size: 13px;
  margin-top: 10px;
}
ul li {
  text-align: center;
  float: left;
  margin-right: 25px;
  height: 40px;
  line-height: 40px;
}
.head-nav {
  background-color: #e3e4e5;
}
.c01 {
  float: left;
}
</style>
